<?php

/**
 * Origin PHP Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://origin.eppbar.org/license/new-bsd.txt
 *
 * @copyright  Copyright (c) 2011 RiverSing International Ltd. (http://eppbar.org)
 * @license    http://origin.eppbar.org/license/new-bsd.txt     New BSD License
 * @version    1.5 
 */

use origin\form\FormParser;

require_once 'origin/form/FormParser.php';

/**
 * Form helper.
 * @author Johnson z <johnson@eppbar.org> 2011-03-12
 */
class FormHelper
{

    /**
     * Parse form varibles.
     * Pass an array of FormField and a data entity object into this method,
     * if success, the entity contains the form data,
     * if fail, the entity contains the form data and error message.
     * If fail and if form hint entity is not empty, the error message will extract into form hint and,
     * form data will be filted by htmlspecialchars() function.
     * @param Array of FormField $form_fields or FormField if only one form field
     * @param Object $form_data
     * @param Object $form_hint
     * @return FALSE if success, or TRUE if error occurred.
     */
    public static function parseForm($form_fields, $form_data, $form_hint = FALSE)
    {
        $fp = new FormParser();
        try
        {
            $fp->parse($form_fields, $form_data);
            return FALSE;
        }
        catch (Exception $e)
        {
            if ($form_hint)
            {
                $fp->setFormErrorHint($form_hint);
                $fp->makeFormDataHtmlSafe($form_fields, $form_data);
            }
            return TRUE;
        }
    }

    /**
     * Check if is post data exists.
     */
    public static function isPost()
    {
        return ! empty($_POST);
    
    }

    /**
     * Check if is post method.
     */
    public static function isPostMethod()
    {
        return (strtolower($_SERVER['REQUEST_METHOD']) === "post") ? TRUE : FALSE;
    }

}

?>